package chenjiuxu.seniorui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.DiscretePathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by 15705 on 2017/12/7.
 * Paint练习
 */
public class DrawView extends View {
//    private Paint paint;
//    private Path path;

    public DrawView(Context context) {
        super(context);
    }

    public DrawView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
//        paint = new Paint();
//        paint.setStyle(Paint.Style.FILL);//设置画笔样式
//        paint.setStrokeWidth(100);//设置画笔宽度
//        paint.setAntiAlias(true);//是否抗锯齿
//        paint.setStrokeCap(Paint.Cap.ROUND);
//        paint.setColor(Color.CYAN);
//        path = new Path();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        drawPathDashPathEffectDemo(canvas);
        canvas.drawLine(50, 50, 500, 50, new Paint());//绘制线段
    }


    /**
     * 绘制线段
     */
    private void drawStroke(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);//设置画笔样式
        paint.setStrokeWidth(20);//设置画笔宽度
        paint.setAntiAlias(true);//是否抗锯齿
        paint.setColor(Color.GRAY);//画笔颜色
        canvas.drawLine(50, 50, 500, 50, paint);//绘制线段

    }

    /**
     * 线冒
     */
    private void drawStrokeCap(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStrokeWidth(80);
        paint.setAntiAlias(true);
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.BUTT);// 无线帽
        canvas.drawLine(100, 200, 400, 200, paint);
        paint.setStrokeCap(Paint.Cap.SQUARE);
        canvas.drawLine(100, 400, 400, 400, paint);// 方形线帽
        paint.setStrokeCap(Paint.Cap.ROUND);
        canvas.drawLine(100, 600, 400, 600, paint);// 圆形线帽

    }


    /**
     * 连线转角样式
     */
    private void drawJiao(Canvas canvas) {

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);//设置画笔样式
        paint.setStrokeWidth(20);//设置画笔宽度
        paint.setAntiAlias(true);//是否抗锯齿
        paint.setColor(Color.GRAY);//画笔颜色


        Path path;
        path = new Path();
        path.moveTo(50, 50);
        path.lineTo(300, 50);
        path.lineTo(50, 200);
        paint.setStrokeJoin(Paint.Join.MITER);//锐角
        canvas.drawPath(path, paint);


        path = new Path();
        path.moveTo(50, 150);
        path.lineTo(300, 150);
        path.lineTo(50, 300);
        paint.setStrokeJoin(Paint.Join.ROUND);//锐角
        canvas.drawPath(path, paint);

        path = new Path();
        path.moveTo(150, 250);
        path.lineTo(300, 250);
        path.lineTo(150, 400);
        paint.setStrokeJoin(Paint.Join.BEVEL);//直线
        canvas.drawPath(path, paint);

    }

    /**
     *转弯半径
     * */
    private void drawCornerPathEffectDemo(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);//设置画笔样式
        paint.setStrokeWidth(20);//设置画笔宽度
        paint.setAntiAlias(true);//是否抗锯齿
        paint.setColor(Color.GRAY);//画笔颜色

        Path path = new Path();
        path.moveTo(100, 600);
        path.lineTo(400, 100);
        path.lineTo(700, 900);

        canvas.drawPath(path, paint);

        paint.setPathEffect(new CornerPathEffect(200));
        canvas.save();
        canvas.translate(0, 150);
        canvas.drawPath(path, paint);
    }

    /**
     *转弯半径对比
     * */
    private void drawCornerPathEffect(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);//设置画笔样式
        paint.setStrokeWidth(20);//设置画笔宽度
        paint.setAntiAlias(true);//是否抗锯齿
        paint.setColor(Color.GRAY);//画笔颜色

        Path path = new Path();
        path.moveTo(100, 600);
        path.lineTo(400, 100);
        path.lineTo(700, 900);

        canvas.drawPath(path, paint);
        paint.setColor(Color.RED);
        paint.setPathEffect(new CornerPathEffect(100));
        canvas.drawPath(path, paint);

        paint.setPathEffect(new CornerPathEffect(200));
        paint.setColor(Color.YELLOW);
        canvas.drawPath(path, paint);
    }

    /**
     * 路径虚线效果
     */
    private void drawDashPathEffectDemo(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);//设置画笔样式
        paint.setStrokeWidth(20);//设置画笔宽度
        paint.setAntiAlias(true);//是否抗锯齿
        paint.setColor(Color.GRAY);//画笔颜色

        Path path = new Path();
        path.moveTo(100, 600);
        path.lineTo(400, 100);
        path.lineTo(700, 900);

        canvas.translate(0, 100);
        paint.setPathEffect(new DashPathEffect(new float[]{20, 5, 5, 20}, 0));
        canvas.drawPath(path, paint);
    }



    private void drawDiscretePathEffectDemo(Canvas canvas){
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);//设置画笔样式
        paint.setStrokeWidth(20);//设置画笔宽度
        paint.setAntiAlias(true);//是否抗锯齿
        paint.setColor(Color.GRAY);//画笔颜色

        Path path = new Path();
        path.moveTo(100, 600);
        path.lineTo(400, 100);
        path.lineTo(700, 900);

        canvas.drawPath(path,paint);
        /**
         * 把原有的路线,在指定的间距处插入一个突刺
         * 第一个这些突出的“杂点”的间距,值越小间距越短,越密集
         * 第二个是突出距离
         */
        canvas.translate(0,200);
        paint.setPathEffect(new DiscretePathEffect(2,5));
        canvas.drawPath(path,paint);

        canvas.translate(0,200);
        paint.setPathEffect(new DiscretePathEffect(6,5));
        canvas.drawPath(path,paint);


        canvas.translate(0,200);
        paint.setPathEffect(new DiscretePathEffect(6,15));
        canvas.drawPath(path,paint);
    }


    private void drawPathDashPathEffectDemo(Canvas canvas){
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);//设置画笔样式
        paint.setStrokeWidth(20);//设置画笔宽度
        paint.setAntiAlias(true);//是否抗锯齿
        paint.setColor(Color.GRAY);//画笔颜色

        Path path = new Path();
        path.moveTo(100, 600);
        path.lineTo(400, 100);
        path.lineTo(700, 900);
        canvas.drawPath(path,paint);

        canvas.translate(0,200);

        paint.setPathEffect(new PathDashPathEffect(getStampPath(),35,0, PathDashPathEffect.Style.MORPH));
        canvas.drawPath(path,paint);

        canvas.translate(0,200);
        paint.setPathEffect(new PathDashPathEffect(getStampPath(),35,0, PathDashPathEffect.Style.ROTATE));
        canvas.drawPath(path,paint);

        canvas.translate(0,200);
        paint.setPathEffect(new PathDashPathEffect(getStampPath(),35,0, PathDashPathEffect.Style.TRANSLATE));
        canvas.drawPath(path,paint);
    }

    private Path getStampPath(){
        Path path  = new Path();
        path.moveTo(0,20);
        path.lineTo(10,0);
        path.lineTo(20,20);
        path.close();

        path.addCircle(0,0,3, Path.Direction.CCW);

        return path;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        float X = event.getX();
//        float Y = event.getY();
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                path.moveTo(X, Y);
//                break;
//            case MotionEvent.ACTION_MOVE:
//                path.lineTo(X, Y);
//                break;
//        }
//        invalidate();
        return true;
    }
}
